/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.component.tabbedPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;
/**
 * 说明：
 * 重写JPanel
 * @Author：李智深
 * @Date：2012-07-04
 */
public class RPTabComponent extends JPanel {

	private static final long serialVersionUID = 1L;
    private TexturePaint selectedPaint = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.TabbedPane_header_background).getImage());
    private TexturePaint unselectedPaint = Util.createTexturePaint(ImageFactory.loadImage(ImageFactory.TabbedPane_header_unselected_background).getImage());
    private ImageIcon icon = ImageFactory.loadImage(ImageFactory.TabbedPane_close);
    private ImageIcon pressedIcon = ImageFactory.loadImage(ImageFactory.TabbedPane_close_pressed);
    private Image unselectedLeftImage = ImageFactory.loadImage(ImageFactory.TabbedPane_header_unselected_background_left).getImage();
    private Image unselectedRightImage =ImageFactory.loadImage(ImageFactory.TabbedPane_header_unselected_background_right).getImage();
    private Image selectedLeftImage = ImageFactory.loadImage(ImageFactory.TabbedPane_header_selected_background_left).getImage();
    private Image selectedRightImage = ImageFactory.loadImage(ImageFactory.TabbedPane_header_selected_background_right).getImage();
    
    private JButton btnClose = new JButton();
    private JLabel lbTitle = new JLabel();
    private RPTabbedPane tab = null;
    private Color selectedTitleColor = new Color(0, 105, 154);
    private Color unselectedTitleColor = new Color(100, 101, 102);
    private Border border = BorderFactory.createEmptyBorder(0, 5, 0, 5);

    public RPTabComponent(RPTabbedPane tab) {
        this.tab = tab;
        init();
    }

    private void init() {
        btnClose.setIcon(icon);
        btnClose.setPressedIcon(pressedIcon);
        btnClose.setToolTipText("关闭");
        btnClose.setMargin(Util.ZERO_INSETS);
        btnClose.setFocusPainted(false);
        btnClose.setBorder(BorderFactory.createEmptyBorder(0, 3, 1, 3));
        btnClose.setContentAreaFilled(false);
        btnClose.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                closeTab();
            }
        });

        lbTitle.setOpaque(false);
        lbTitle.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        lbTitle.setVerticalAlignment(SwingConstants.CENTER);
        lbTitle.setHorizontalAlignment(SwingConstants.CENTER);
        lbTitle.setFont(Util.FONT_12_BOLD);
        lbTitle.setBackground(Color.RED);

        this.setLayout(new BorderLayout());
        this.add(btnClose, BorderLayout.EAST);
        this.add(this.lbTitle, BorderLayout.CENTER);
        this.setBorder(border);
        this.setOpaque(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        if (isTabSelected()) {
            g2d.drawImage(selectedLeftImage, 0, 0, null);
            g2d.setPaint(selectedPaint);
            int x = selectedLeftImage.getWidth(null);
            int y = 0;
            int width = getWidth() - x - selectedRightImage.getWidth(null);
            int height = getHeight();
            g2d.fillRect(x, y, width, height);
            g2d.drawImage(selectedRightImage, x + width, 0, null);
        } else {
            g2d.drawImage(unselectedLeftImage, 0, 0, null);
            g2d.setPaint(unselectedPaint);
            int x = unselectedLeftImage.getWidth(null);
            int y = 0;
            int width = getWidth() - x - selectedRightImage.getWidth(null);
            int height = getHeight();
            g2d.fillRect(x, y, width, height);
            g2d.drawImage(unselectedRightImage, x + width, 0, null);

            //draw a line in the bottom.
//            g2d.setColor(Util.TAB_BOTTOM_LINE_COLOR);
//            int lineY = this.getHeight() - 1;
//            g2d.drawLine(0, lineY, getWidth(), lineY);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        int width = super.getPreferredSize().width+10;
        if (!this.isTabSelected()) {
//            width = Math.min(width, tab.getPreferredUnselectedTabWidth());
        }
        int height = tab.getPreferredTabHeight();
        return new Dimension(width, height);
    }

    public boolean isTabSelected() {
        int index = tab.indexOfTabComponent(this);
        int selectedIndex = tab.getSelectedIndex();
        return selectedIndex == index;
    }

    public void setTitle(String title) {
        this.lbTitle.setText(title);
    }

    public void updateSelection(boolean selected) {
        if (selected) {
            this.lbTitle.setForeground(selectedTitleColor);
        } else {
            this.lbTitle.setForeground(unselectedTitleColor);
        }
        this.btnClose.setVisible(true);
    }

    private void closeTab() {
        int index = tab.indexOfTabComponent(this);
        tab.removeTabAt(index);
//        if (tab.get)
    }
}
